package org.luckyjourney.service.navigationBar.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.luckyjourney.config.QiNiuConfig;
import org.luckyjourney.entity.File;
import org.luckyjourney.entity.booking.NavigationBar;
import org.luckyjourney.entity.booking.dto.NavigationBarDTO;
import org.luckyjourney.entity.booking.vo.NavigationBarVO;
import org.luckyjourney.exception.BaseException;
import org.luckyjourney.mapper.navigationBar.NavigationBarMapper;
import org.luckyjourney.service.FileService;
import org.luckyjourney.service.navigationBar.NavigationBarService;
import org.luckyjourney.util.PageResult;
import org.luckyjourney.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NavigationBarServiceImpl extends ServiceImpl<NavigationBarMapper, NavigationBar> implements NavigationBarService {
    @Autowired
    NavigationBarMapper navigationBarMapper;
    @Autowired
    FileService fileService;

    @Override
    public String insert(NavigationBar navigationBar) {
        int insert = navigationBarMapper.insert(navigationBar);
        if (insert > 0) {
            return "添加成功";
        }
        return "添加失败";

    }

    @Override
    public Result delect(NavigationBar navigationBar) {
        Long id = navigationBar.getId();
        if (ObjectUtil.isEmpty(id)) {
            throw new BaseException("id不能为空");
        }
        int i = navigationBarMapper.deleteById(id);

        if (i > 0) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @Override
    public Result updates(NavigationBar navigationBar) {
        Long id = navigationBar.getId();
        if (ObjectUtil.isEmpty(id)) {
            throw new BaseException("id不能为空");
        }


        int i = navigationBarMapper.updateById(navigationBar);

        if (i > 0) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @Override
    public Result selectId(NavigationBar navigationBar) {
        Long id = navigationBar.getId();
        if (ObjectUtil.isEmpty(id)) {
            throw new BaseException("id不能为空");
        }

        //id  name  pinyin
        NavigationBar navigationBar1 = navigationBarMapper.selectById(id);
        if (ObjectUtil.isEmpty(navigationBar1)) {
            return Result.error("数据不存在");
        }
        NavigationBarVO vo = new NavigationBarVO();
        BeanUtils.copyProperties(navigationBar1,vo);
        if (ObjectUtil.isNotEmpty(navigationBar1.getChooseImage())) {
            File file = fileService.getById(navigationBar1.getChooseImage());
            String c = QiNiuConfig.CNAME + "/" + file.getFileKey();
            vo.setChooseImage(c);
            vo.setC(file.getId());

        }
        if (ObjectUtil.isNotEmpty(navigationBar1.getNotChooseImage())) {
            File file = fileService.getById(navigationBar1.getNotChooseImage());
            String n = QiNiuConfig.CNAME + "/" + file.getFileKey();
            vo.setNotChooseImage(n);
            vo.setN(file.getId());
        }

        return Result.success(vo);
    }

    @Override
    public PageResult selectPage(NavigationBarDTO navigationBarDTO) {
        int page = navigationBarDTO.getPage();

        int pageSize = navigationBarDTO.getPageSize();

        if (ObjectUtil.isEmpty(page) && ObjectUtil.isEmpty(pageSize)) {
            throw new BaseException("页码数和每页条数不能为空");
        }
        //获得每页条数
        PageHelper.startPage(page, pageSize);


        List<NavigationBarVO> voList = navigationBarMapper.pageselect(navigationBarDTO);
        Page<NavigationBarVO> p = (Page<NavigationBarVO>) voList;

        ArrayList<NavigationBarVO> list = new ArrayList<>();
        List<NavigationBarVO> result1 = p.getResult();
        for (NavigationBarVO navigationBarVO : result1) {
            NavigationBarVO vo = new NavigationBarVO();
            BeanUtils.copyProperties(navigationBarVO,vo);
            if (ObjectUtil.isNotEmpty(navigationBarVO.getChooseImage())) {
                File file = fileService.getById(navigationBarVO.getChooseImage());
                String c = QiNiuConfig.CNAME + "/" + file.getFileKey();
                vo.setChooseImage(c);
                vo.setC(file.getId());

            }
            if (ObjectUtil.isNotEmpty(navigationBarVO.getNotChooseImage())) {
                File file = fileService.getById(navigationBarVO.getNotChooseImage());
                String n = QiNiuConfig.CNAME + "/" + file.getFileKey();
                vo.setNotChooseImage(n);
                vo.setN(file.getId());
            }
            list.add(vo);
        }

        PageResult result = new PageResult(p.getTotal(), list);
        return result;
    }
}
